package com.woniuxy.controller;

import com.woniuxy.dao.AccidentRecordMapper;
import com.woniuxy.pojo.Car;
import com.woniuxy.pojo.Dictionary;
import com.woniuxy.pojo.Driver;
import com.woniuxy.pojo.AccidentRecord;
import com.woniuxy.service.AccidentRecordService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.AccidentRecordVo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AccidentRecordController extends BaseController{
    private Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AccidentRecordService accidentRecordService;

    @ApiOperation(value = "查找事故记录",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "查找事故记录")
    @GetMapping("/accidentRecords")
    public DataGridView findAccidentRecordsByPage(AccidentRecordVo accidentRecordVo){
        logger.debug("事故记录："+accidentRecordVo.toString());
        return accidentRecordService.findByPage(accidentRecordVo);
    }

    @ApiOperation(value = "删除事故记录",httpMethod = "DELETE",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "删除事故记录")
    @DeleteMapping(value = "/accidentRecords/{id}")
    public ResultDto removeAccidentRecord(@PathVariable("id") int id){
        try {
            accidentRecordService.removeById(id);
            return ResultDto.ACCIDENT_RECORD_DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.ACCIDENT_RECORD_DELETE_SUCCESS;
        }
    }

    @ApiOperation(value = "批量删除事故记录",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "批量删除事故记录")
    @GetMapping(value = "accidentRecords/deleteBatch")
    public ResultDto removeBathAccidentRecords(@RequestParam("ids[]") Integer[] ids){
        try {
            accidentRecordService.removeBatch(ids);
            return ResultDto.ACCIDENT_RECORD_DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.ACCIDENT_RECORD_DELETE_SUCCESS;
        }
    }

    @ApiOperation(value = "添加事故记录",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "添加事故记录")
    @PostMapping(value = "/accidentRecords")
    public ResultDto addAccidentRecord(AccidentRecord accidentRecord){
        try {
            boolean flag=accidentRecordService.add(accidentRecord);
            if (flag) {
                return ResultDto.ACCIDENT_RECORD_ADD_SUCCESS;
            } else {
                return ResultDto.ACCIDENT_RECORD_ADD_FAIL;
            }
        } catch (Exception e){
            e.printStackTrace();
            return ResultDto.ACCIDENT_RECORD_ADD_FAIL;
        }
    }

    @ApiOperation(value = "编辑事故记录",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "编辑事故记录")
    @PostMapping(value = "/accidentRecords.do")
    public ResultDto updateAccidentRecord(AccidentRecord accidentRecord){
        try {
            boolean flag=accidentRecordService.update(accidentRecord);
            if (flag) {
                return ResultDto.ACCIDENT_RECORD_UPDATE_SUCCESS;
            } else {
                return ResultDto.ACCIDENT_RECORD_UPDATE_FAIL;
            }
        } catch (Exception e){
            e.printStackTrace();
            return ResultDto.ACCIDENT_RECORD_UPDATE_FAIL;
        }
    }
}
